Animation animation1, animation2, animation3, animation4;
Phase1 phase1;
PImage grass;

int xpos = 0;
int ypos = 0;
float drag = 30.0;
int vel = 3;
int displayW = 380;
int displayH = 550;
int right = 0, left =1, up = 0, down = 0;
int[] input_flags = new int[4];
int r_coll=0, l_coll=0, d_coll=0, u_coll =0;
int scene = 1;

void setup() {
  size(displayWidth, displayHeight, P3D);
  background(0, 180, 0);
  frameRate(24);
  animation1 = new Animation("hd_right_", 3);
  animation2 = new Animation("hd_left_", 3);
  animation3 = new Animation("hd_up_", 3);
  animation4 = new Animation("hd_down_", 3);
  phase1 = new Phase1();
  grass = loadImage("grass.gif");
  xpos = displayWidth - animation2.getWidth();
  ypos = displayHeight - animation2.getHeight() - 55;
}

void drawTexture() {
  beginShape();
  texture(grass);
  vertex(0, 0, 0, 0);
  vertex(displayWidth, 0, grass.width, 0);
  vertex(displayWidth, displayHeight, grass.width, grass.height);
  vertex(0, displayHeight, 0, grass.height);
  endShape();
}
void draw() { 
  
  if(r_coll == 1 || l_coll == 1 || u_coll == 1 || d_coll == 1) {
    scene++;
  }
  if(scene == 1) {
    stayInsideScreen();
    phase1.drawPath();
    phase1.collision(xpos, ypos, animation1.getHeight(), animation1.getWidth());
    checkInputFlags();
    // Display the sprite at the position xpos, ypos
    if (left == 1) {
      background(0, 180, 0);
      drawTexture();
      phase1.drawPath();
      xpos-=vel;
      animation2.display(xpos, ypos);
      r_coll = 0;
      u_coll = 0;
      d_coll = 0;
    }
    else if (right == 1) {
      background(0, 180, 0);
      drawTexture();
      phase1.drawPath(); 
      xpos+=vel;
      animation1.display(xpos, ypos);
      l_coll = 0;
      u_coll = 0;
      d_coll = 0;
    }
    else if (up == 1) {
      background(0, 180, 0);
      drawTexture();
      phase1.drawPath();
      ypos-=vel;
      animation3.display(xpos, ypos);
      r_coll = 0;
      l_coll = 0;
      d_coll = 0;
    }
    else if (down == 1) {
      background(0, 180, 0);
      drawTexture();
      phase1.drawPath();
      ypos+=vel;
      animation4.display(xpos, ypos);
      r_coll = 0;
      u_coll = 0;
      l_coll = 0;
    }
  }
  if (scene == 2) {
    background(255,0,0);
    scene = 1;
  }
}

void mouseDragged() {

  if (mouseX > ((int) xpos + animation2.getWidth())) {
    if ((mouseY < ((int) ypos + animation2.getHeight()))
      && (mouseY > ypos)) 
      right = 1;
    left = 0;
    down = 0;
    up = 0;
  }
  else if (mouseX < ((int) xpos)) {
    if ((mouseY < ((int) ypos + animation2.getHeight()))
      && (mouseY > ypos)) {
      left = 1;
      right = 0;
      down = 0;
      up = 0;
    }
  }
  else if (mouseY > ((int) ypos + animation2.getHeight())) {
    if ((mouseX < ((int) xpos + animation2.getWidth()))
      && (mouseX > xpos)) {
      left = 0;
      right = 0;
      down = 1;
      up = 0;
    }
  }
  else if (mouseY < ((int) ypos)) {
    if ((mouseX < ((int) xpos + animation2.getWidth()))
      && (mouseX > xpos)) {
      left = 0;
      right = 0;
      down = 0;
      up = 1;
    }
  }
}

void checkInputFlags() {
  input_flags = phase1.getInputs(); 
  r_coll = input_flags[0];
  l_coll = input_flags[1];
  u_coll = input_flags[2];
  d_coll = input_flags[3];
}

//This function is to don't let the pacman go outside the window
void stayInsideScreen() {
  int spr_width = animation1.getWidth();
  int spr_height = animation1.getHeight();

  if (xpos<0) 
    xpos = 0;
  if (xpos> (displayWidth - spr_width))
    xpos = displayWidth - spr_width;
  if (ypos<0)
    ypos = 0;
  if (ypos > (displayHeight - spr_height) )
    ypos = displayHeight - spr_height;
}

